<template>
  <BasicContainer>
    <div class="avue-crud">
      <ElForm :inline="true">
        <ElFormItem label="菜单名称" prop="menuName">
          <ElInput
            v-model="queryParams.menuName"
            placeholder="请输入菜单名称"
            clearable
            size="small"
            @keyup.enter.native="getList" />
        </ElFormItem>
        <ElFormItem>
          <ElButton type="primary" icon="el-icon-search" @click="getList">搜索</ElButton>

          <ElButton
            v-if="permissions.sys_menu_add"
            icon="el-icon-plus"
            type="primary"
            @click="addOrUpdateHandle(false)">
            添加
          </ElButton>
        </ElFormItem>
      </ElForm>

      <ElTable
        v-loading="loading"
        border
        :data="menuList"
        row-key="id"
        :tree-props="{ children: 'children', hasChildren: 'hasChildrens' }">
        <ElTableColumn prop="name" label="菜单名称" :show-overflow-tooltip="true" width="180" />
        <ElTableColumn prop="icon" label="图标" align="center" width="100">
          <template #default="scope">
            <i :class="scope.row.icon"></i>
          </template>
        </ElTableColumn>
        <ElTableColumn prop="sortOrder" label="排序" width="60" />
        <ElTableColumn prop="path" label="组件路径" :show-overflow-tooltip="true" />
        <ElTableColumn prop="menuType" label="类型" width="80" align="center">
          <template #default="scope">
            <ElTag v-if="scope.row.menuType === '0'" type="success">左菜单</ElTag>
            <ElTag v-if="scope.row.menuType === '2'" type="success">顶菜单</ElTag>
            <ElTag v-if="scope.row.menuType === '1'" type="info">按钮</ElTag>
          </template>
        </ElTableColumn>
        <ElTableColumn prop="keepAlive" label="缓冲" width="80" align="center">
          <template #default="scope">
            <ElTag v-if="scope.row.keepAlive === '0'" type="info">关闭</ElTag>
            <ElTag v-if="scope.row.keepAlive === '1'" type="success">开启</ElTag>
          </template>
        </ElTableColumn>
        <ElTableColumn prop="permission" label="权限标识" :show-overflow-tooltip="true" />
        <ElTableColumn label="操作" align="center" width="200">
          <template #default="scope">
            <ElButton
              v-if="permissions.sys_menu_add"
              size="small"
              type="text"
              icon="el-icon-plus"
              @click="addOrUpdateHandle(false, scope.row.id)">
              新增
            </ElButton>
            <ElButton
              v-if="permissions.sys_menu_edit"
              size="small"
              type="text"
              icon="el-icon-edit"
              @click="addOrUpdateHandle(true, scope.row.id)">
              修改
            </ElButton>
            <ElButton
              v-if="permissions.sys_menu_del"
              size="small"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)">
              删除
            </ElButton>
          </template>
        </ElTableColumn>
      </ElTable>

      <TableForm v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getList" />
    </div>
  </BasicContainer>
</template>

<script>
  import { delObj, fetchMenuTree } from '@/api/admin/menu'
  import TableForm from './menu-form'
  import { mapState } from 'vuex'

  export default {
    name: 'MenuIndex',
    components: { TableForm },
    data() {
      return {
        addOrUpdateVisible: false,
        // 遮罩层
        loading: true,
        // 菜单表格树数据
        menuList: [],
        // 查询参数
        queryParams: {
          parentId: undefined,
          menuName: ''
        }
      }
    },
    computed: {
      ...mapState('user', ['permissions'])
    },
    created() {
      this.getList()
    },
    methods: {
      addOrUpdateHandle(isEdit, id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(isEdit, id)
        })
      },
      getList() {
        this.loading = true
        fetchMenuTree(this.queryParams).then(response => {
          this.menuList = response.data.data
          this.loading = false
        })
      },
      handleDelete(row) {
        this.$confirm(`是否确认删除名称为"${row.name}"的数据项?`, '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            return delObj(row.id)
          })
          .then(data => {
            this.getList()
            this.$message.success(data.data.msg)
          })
      }
    }
  }
</script>
